package com.ruoyi.system.controller;

import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import java.nio.file.*;
import java.io.*;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.system.Control.Customer;
import com.ruoyi.system.Control.ExcelMergeCellHandler;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.QPingjia;
import com.ruoyi.system.service.IQPingjiaService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2024-06-07
 */
@Controller
@RequestMapping("/system/pingjia")
public class QPingjiaController extends BaseController {
    private String prefix = "system/pingjia";

    @Autowired
    private IQPingjiaService qPingjiaService;

    @RequiresPermissions("system:pingjia:view")
    @GetMapping()
    public String pingjia() {
        return prefix + "/pingjia";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @CrossOrigin
    @RequiresPermissions("system:pingjia:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(QPingjia qPingjia) {
        startPage();
        List<QPingjia> list = qPingjiaService.selectQPingjiaList(qPingjia);
        return getDataTable(list);
    }

    /**
     * 获取下载路径
     *
     * @param filename 文件名称
     */
    @CrossOrigin
    public String getAbsoluteFile(String filename) {
        String downloadPath = RuoYiConfig.getDownloadPath() + filename;
        File desc = new File(downloadPath);
        if (!desc.getParentFile().exists()) {
            desc.getParentFile().mkdirs();
        }
        return downloadPath;
    }

    @CrossOrigin
    public static List<Customer> getData() throws Exception {
        List<Customer> data = new ArrayList<>();

        Customer customer = new Customer("JiangXi", "江西电信公司", "江西省南昌市东湖区", "张三", "12345678910", new URL("https://m.360buyimg.com/babel/jfs/t1/221733/11/14107/61280/62fde84dE467522ce/79bbd42aa93f5a83.jpg"));
        data.add(customer);
        Customer customer2 = new Customer("JiangXi", "江西电信公司", "江西省南昌市东湖区", "李四", "15848563521", new URL("https://m.360buyimg.com/babel/jfs/t1/221733/11/14107/61280/62fde84dE467522ce/79bbd42aa93f5a83.jpg"));
        data.add(customer2);

        Customer customer3 = new Customer("GuangDong", "广东电信公司", "广东省广州市花都区", "小明", "15847953624", new URL("https://m.360buyimg.com/babel/jfs/t1/215924/36/19623/23344/62baa985E4df523c6/4893237860b306d6.jpg"));
        data.add(customer3);
        Customer customer4 = new Customer("GuangDong", "广东电信公司", "广东省广州市天河区", "小红", "16849531548", new URL("https://m.360buyimg.com/babel/jfs/t1/189640/15/26493/35837/62baa97eE6abda209/461f91e682d0e81a.jpg"));
        data.add(customer4);
        Customer customer5 = new Customer("GuangDong", "广东电信公司", "广东省广州市天河区", "小华", "16985632481", new URL("https://m.360buyimg.com/babel/jfs/t1/189640/15/26493/35837/62baa97eE6abda209/461f91e682d0e81a.jpg"));
        data.add(customer5);

        Customer customer6 = new Customer("BeiJing", "北京电信公司", "北京市东城区", "姜维", "16598645874", new URL("https://m.360buyimg.com/babel/jfs/t1/31481/11/16081/24873/62baa97dE6f3991d0/94ae13d66b9bbfdd.jpg"));
        data.add(customer6);

        return data;
    }

    /**
     * 导出【评价】列表
     */
    @CrossOrigin
    @RequiresPermissions("system:pingjia:export")
    @Log(title = "【评价】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(QPingjia qPingjia) throws Exception {

        String filename = new String("满意度报表(tmplate)" + ".xlsx");


        Path sourcePath = Paths.get(getAbsoluteFile(filename));
        Path destinationPath = Paths.get(getAbsoluteFile("满意度统计.xlsx"));



        try {
            Files.copy(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING);
            System.out.println("File copied successfully.");
        } catch (IOException e) {
            System.out.println("Error occurred while copying file.");
            e.printStackTrace();
        }

        List data = getData();



        try {
            FileInputStream file = new FileInputStream( getAbsoluteFile("满意度统计.xlsx"));
            Workbook workbook = WorkbookFactory.create(file);
            Sheet sheet = workbook.getSheetAt(0);
            int rowIndex = 3 ;  //从第三行开始写
            for(int i=0; i< data.size(); i++){
                Customer customer = new Customer();
                customer = (Customer) data.get(i);


                Row row = sheet.getRow(i+rowIndex);
                int colIndex = 0 ;
                Cell cell = row.getCell(colIndex++);
                cell.setCellValue(customer.getPersonName());

                cell = row.getCell(colIndex++);
                cell.setCellValue(customer.getAddress());



            }
            Row row = sheet.getRow(0);
            Cell cell = row.getCell(0);

            // 修改单元格的内容
            cell.setCellValue("New Value");

            file.close();

            FileOutputStream outFile = new FileOutputStream( getAbsoluteFile("满意度统计.xlsx"));
            workbook.write(outFile);
            outFile.close();

            System.out.println("Excel file modified successfully.");

        } catch (Exception e) {
            e.printStackTrace();
        }








        return AjaxResult.success("满意度统计.xlsx");
    }

    /**
     * 新增【请填写功能名称】
     */
    @CrossOrigin
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */

    @CrossOrigin
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(QPingjia qPingjia) {
        return toAjax(qPingjiaService.insertQPingjia(qPingjia));
    }

    /**
     * 修改【请填写功能名称】
     */

    @CrossOrigin
    @GetMapping("/edit/{telPingjiaId}")
    public String edit(@PathVariable("telPingjiaId") Long telPingjiaId, ModelMap mmap) {
        QPingjia qPingjia = qPingjiaService.selectQPingjiaByTelPingjiaId(telPingjiaId);
        mmap.put("qPingjia", qPingjia);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */

    @CrossOrigin
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(QPingjia qPingjia) {
        return toAjax(qPingjiaService.updateQPingjia(qPingjia));
    }

    /**
     * 删除【请填写功能名称】
     */
    @CrossOrigin
    @RequiresPermissions("system:pingjia:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(qPingjiaService.deleteQPingjiaByTelPingjiaIds(ids));
    }
}
